#!/usr/bin/env python
# encoding: utf-8
"""
common.py

Descripton:
Common data and functions accessable to all files.
Contains image loading and imports all the math functions for easy access
"""

import pygame
import os
import math

pygame.font.init()

# Debug mode constant boolean
DEBUG = True
# Default max fps
FPS = 40

# directory to load resources
map_dir        ="data/levels/"
root_image_dir ="data/images/"
root_font_dir  ="data/fonts/"
root_sound_dir ="data/sounds/"

# dictionaries
images= {None:None}
fonts=  {None:None}
sounds= {None:None}

# extensions to be loaded
image_extensions=[".png",".jpg",".bmp",".gif"]
font_extensions=[".ttf"]
sound_extensions=[".wav"]

#the font sizes to be used. font names in the dictionary are assembled like: common.fonts["fontname24"]
fontsize = [24,20,22,12]

def d_print(text):
    if DEBUG:
        print text


def load_images(image_dir=root_image_dir):
    """Loads all images from the image directory and puts them into the image dictionary."""
    for filename in os.listdir(image_dir):
        # test if extensions are image extensions
        for extension in image_extensions:
            if filename[-4:] == extension:
                try:
                    images[filename[:-4]]=pygame.image.load(image_dir+filename).convert_alpha()
                except:
                    d_print("Failed to load "+image_dir+filename)
        # if file is a directory, recurse 
        if filename.find(".") == -1:
            try:
                load_images(image_dir+filename+"/")
            except:
                pass
                
def load_sounds(sound_dir=root_sound_dir):
    """Loads all images from the image directory and puts them into the image dictionary."""
    for filename in os.listdir(sound_dir):
        # test if extensions are image extensions
        for extension in sound_extensions:
            if filename[-4:] == extension:
                sounds[filename[:-4]]=pygame.mixer.Sound(sound_dir+filename)
                d_print("Failed to load "+sound_dir+filename)
        # if file is a directory, recurse 
        if filename.find(".") == -1:
            try:
                load_sounds(image_dir+filename+"/")
            except:
                pass                

def load_fonts(image_dir=root_font_dir):
    """Loads all fonts from the font directory and puts them into the font dictionary."""
    for filename in os.listdir(image_dir):
        # test if extensions are image extensions
        for extension in font_extensions:
            if filename[-4:] == extension:
                try:
                	for size in fontsize:
                	    fonts[filename[:-4]+str(size)]=pygame.font.Font(image_dir+filename,size)	    
                except:
                    d_print("Failed to load "+image_dir+filename)
        # if file is a directory, recurse 
        if filename.find(".") == -1:
            try:
                load_images(image_dir+filename+"/")
            except:
                pass
                
def get_dist_sq(x,y):
    return x*x+y*y

def get_dist(x,y):
    return math.sqrt(x*x+y*y)
    
def get_angle(x,y):
    return math.atan2(x,y)

def get_xy(angle,mag=1):
    return (math.sin(angle)*mag,math.cos(angle)*mag)    